package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.PageReservationVo;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletResponse;

/**
 * 预约信息Service业务层处理
 * 
 * @author zhai
 * @date 2025-01-10
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    /**
     * 查看取消的次数
     *
     * @param userId
     * @return
     */
    @Override
    public int getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        //2025/1/10 00:00:00
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        //2025/1/11 00:00:00
        LocalDateTime endTime = startTime.plusDays(1);

        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper = lambdaQueryWrapper.eq(Reservation::getId, userId)
                .between(Reservation::getTime, startTime, endTime);

        return (int) count(lambdaQueryWrapper);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<TimeCountVo> appointmentsRemaining(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        //2025/1/10 00:00:00
        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
        //2025/1/11 00:00:00
        LocalDateTime endTime = startTime.plusDays(1);
        List<TimeCountVo> timeCount = reservationMapper.appointmentsRemaining(startTime,endTime);
        return timeCount;
    }

    /**
     * 新增预约
     *
     * @return
     */
    @Override
    public int saveReservation(Reservation reservation) {
        Long userId = UserThreadLocal.getUserId();
        Reservation res = BeanUtil.toBean(reservation, Reservation.class);
        res.setCreateBy(String.valueOf(userId));
        res.setUpdateBy(String.valueOf(userId));
        res.setStatus(0);
        try {
            return save(res) ? 1 : 0;
        } catch (Exception e) {
            throw new BaseException("不能重复预约哦");
        }
    }

    /**
     * 分页查询预约
     *
     * @param  pageNum, pageSize, status
     * @return
     */
    @Override
    public TableDataInfo<Reservation> pageReservation(Integer pageNum,Integer pageSize,Integer status) {
        //获取当前用户id
        Page<Reservation> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper = lambdaQueryWrapper.eq(status != null,Reservation::getStatus,status);
        page = page(page,lambdaQueryWrapper);
        return getTableDateInfo(page);
    }

    private TableDataInfo<Reservation> getTableDateInfo(Page<Reservation> page) {
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setCode(HttpServletResponse.SC_OK);
        tableDataInfo.setTotal(page.getTotal());
        return  tableDataInfo;
    }


}
